package com.crazy.util;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

/**
 * @author panyonghui
 * @describe
 * @date 2022/5/11 17:09
 */
public class SftpUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(SftpUtil.class);

    private static Properties properties = null;

    public static ChannelSftp getconnect(String env) throws IOException {
        if (properties == null) {
            properties = new Properties();
            properties.load(SftpUtil.class.getResourceAsStream("ftp-" + env + ".properties"));
        }
        String host = properties.getProperty("host");
        int port = (int) properties.get("port");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            Session sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            LOGGER.info("sftp登录成功！");
        } catch (Exception e) {
            LOGGER.error("sftp登录失败！"+e.getMessage(),e);
        }
        return sftp;
    }

    public static void disconnect(ChannelSftp sftp) {
        try {
            if (sftp != null) {
                Session session = sftp.getSession();
                if(session.isConnected()){
                    session.disconnect();
                }
            }
        } catch (JSchException e) {
            LOGGER.error("关闭sftp会话异常:"+e.getMessage(),e);
        }
    }

    public static int upload(ChannelSftp sftp,List<LocalRemoteInfo> localRemoteInfos){
        int sucNum = 0;
        if(null == sftp || !sftp.isConnected()){
            LOGGER.info("sftp未连接！");
            return sucNum;
        }
        try {
            for (LocalRemoteInfo localRemoteInfo : localRemoteInfos) {
                sftp.cd(localRemoteInfo.getRemotePath());
                sftp.put(localRemoteInfo.getLocalPath(), Paths.get(localRemoteInfo.getLocalPath()).toFile().getName());
                sucNum++;
            }
        }catch (SftpException e){
            LOGGER.error("文件上传异常："+e.getMessage(),e);
        }
        return sucNum;
    }

    public static int download(ChannelSftp sftp,List<LocalRemoteInfo> localRemoteInfos){
        int sucNum = 0;
        if(null == sftp || !sftp.isConnected()){
            LOGGER.info("sftp未连接！");
            return sucNum;
        }
        try {
            for (LocalRemoteInfo localRemoteInfo : localRemoteInfos) {
                sftp.get(localRemoteInfo.getRemotePath(), localRemoteInfo.getLocalPath());
                sucNum++;
            }
        } catch (SftpException e) {
            LOGGER.error("文件下载异常："+e.getMessage(),e);
        }
        return sucNum;
    }

    public static class LocalRemoteInfo{
        private String localPath;
        private String remotePath;

        public String getLocalPath() {
            return localPath;
        }

        public void setLocalPath(String localPath) {
            this.localPath = localPath;
        }

        public String getRemotePath() {
            return remotePath;
        }

        public void setRemotePath(String remotePath) {
            this.remotePath = remotePath;
        }
    }
}
